C:::::::::::::::::::::OPEN_FILE::::::::::::::::::::::::::::::::::::
C.... This routine is responsible for processing the control file(eg.HEUVAC.bat)
C.... which is specified in first argument.
C.... after opening the control file,this routine calls GET_INOUT_FILES()
C.... to extract all I/O files from the control file and open them with associated 
C.... unit numbers.
C.... Finally,this routine calls GET_CONTR_PARAMS() to extract some paramter 
C.... values from control file and write them to a file associated with unit 5.
C....

      SUBROUTINE OPEN_FILE() 
          
      USE DFLIB                     !use visual digital fortran library
                                         
      INTEGER       INPUT           !Input unit for control file
      INTEGER       OUTPUT          !Output unit for log file
      INTEGER       DATAUNIT        !Unit number for data file
      CHARACTER*60  ARG_1           !First command line argument
      CHARACTER*60  FILENAME        !User input file name
      CHARACTER     ANSWER1         !User input for "y"or "n"
      CHARACTER*60  ARG_2           !Second command line argument
      INTEGER       FLAG            !0:polwinds;1:equwinds;2:dhdu
      
      !... the following three lines are not standard Fortran 
      INTEGER(2) n1, status         !n1:indicate which command line argument
                                    !status: needed by GETARG()
      !... get first command line argument
      n1 = 1                     
      CALL GETARG(n1, ARG_1, status) 
      FILENAME=ARG_1 
      !... get second command line argument
      n2 = 2
      CALL GETARG(n2,ARG_2, status)
      FLAG=0
      IF(INDEX(ARG_2,'EQWIND').NE.0) THEN
          FLAG=1
      ELSE IF (INDEX(ARG_2,'DHDU-NORTH').NE.0) THEN
          FLAG=2
      ELSE IF (INDEX(ARG_2,'DHDU-SOUTH').NE.0) THEN
          FLAG=3
      ENDIF  
                                 
      !... open input control file
        INPUT=49                      
      OPEN(INPUT,FILE=FILENAME,STATUS='OLD',ERR=350)       
      !...extract I/O files and their unit numbers from .RUN file 
      
      CALL GET_INOUT_FILES(INPUT,FLAG)
      
      !... rewind .RUN file and extract parameters from it and  
      !... store these parameters into the file which is associated with unit 5
      REWIND(INPUT)
      CALL GET_CONTR_PARAMS(INPUT,5)
      REWIND(5)
      GO TO 400
          
 350  WRITE(*,*) 'FILE', FILENAME ,'Does NOT EXIST!'
 400       CLOSE(INPUT)
      END


C:::::::::::::::::::::GET_INOUT_FILES::::::::::::::::::::::::::::::::::::
C.... This routine is used to extract input and output files from the
C.... control file (eg.HEUVAC.bat) and open them.
C.... This routine is called in the OPEN_FILE() routine. 
C.... ASSUME: each I/O file and its unit number is contained in line starting 
C.... with "$" and there is an "assign" or "ASSIGN" in front of the line.
C....
      SUBROUTINE GET_INOUT_FILES(INUNIT,FLAG)     
                                                                                                                                 
      INTEGER        INUNIT                  !... Input unit in
      INTEGER        OUTUNIT                 !... Output unit for debugging
      INTEGER        FLAG                    !... 0:polwinds;1:equwinds;2:dhdu                     
      CHARACTER*200  CH_ARRAY                !... Character array
      CHARACTER*60   DIR                     !... Directory name 
      CHARACTER*60   FILENAME                !... File name extracted
      CHARACTER*10   UNIT                    !... Unit string extracted
      INTEGER        UNITNO                  !... Unit number
      INTEGER        LENGTH                  !... length of unit string 
      INTEGER        DIRFLAG                 !... flag to indicate directory
      INTEGER        DIRLEN                  !... directory length
      
      DATA OUTUNIT/0/   !..change to 100 for debug writes 
 50   READ(INUNIT,'(A200)',END=60) CH_ARRAY  !... read each line from input file
      
      DIRFLAG=0                              !... initial directory flag=0
      
      !... skip all starting blanks in each line and blank lines
      DO I=1,100
      IF (CH_ARRAY(I:I).NE.' ') GO TO 55
      ENDDO
      GO TO 50
      !... identify the lines containing '$'
 55   IF(CH_ARRAY(I:I).EQ.'$') THEN
         DO I1=I+1,100
            IF (CH_ARRAY(I1:I1).NE.' ') GO TO 57 
         ENDDO
      
      !... check the line contains "assign" or not
 57      IF(CH_ARRAY(I1:I1+6).EQ.'ASSIGN'.
     >      OR.CH_ARRAY(I1:I1+6).EQ.'assign') THEN
               DO J=I1+7,100 
                   IF (CH_ARRAY(J:J).NE.' ') GO TO 80
               ENDDO
      !... check the line contains "IF", "EQWIND","THEN" or not
         ELSE IF (FLAG.EQ.1.AND.INDEX(CH_ARRAY,'IF').NE.0.AND.
     >            INDEX(CH_ARRAY,'EQWIND').NE.0.AND.
     >              INDEX(CH_ARRAY,'THEN').NE.0.AND.
     >                INDEX(CH_ARRAY,'ASSIGN').NE.0) THEN
                  I1=INDEX(CH_ARRAY,'ASSIGN')

                  DO J=I1+7,100 
                      IF (CH_ARRAY(J:J).NE.' ') GO TO 80
                  ENDDO
               
 80            DO K=J,100
      !... use ":" to identify there is directory info before file name
                   IF (CH_ARRAY(K:K).EQ.':')THEN
                        DIRFLAG=1
                        GO TO 86
                   ENDIF
               ENDDO
               K=J
      !...  find out directory name and length

 86            IF(K.GT.J) THEN
                   DIR=CH_ARRAY(J:K-1)
                   DIRLEN=K-J
                   IF (OUTUNIT.EQ.100) WRITE(OUTUNIT,*)'DIR=',DIR
               ELSE
                   DIR=''
                   DIRLEN=0
               ENDIF
               DO K1=K,100
                   IF (CH_ARRAY(K1:K1).EQ.' ')GO TO 90
               ENDDO

      !...  add directory name to the file name                
 90               IF (DIRFLAG.EQ.1) THEN   
                     FILENAME(1:DIRLEN)=DIR
                     FILENAME(1+DIRLEN:1+DIRLEN)='\'
                     FILENAME(1+DIRLEN+1:)=CH_ARRAY(K+1:K1-1)
               ELSE
                     FILENAME(1:)=CH_ARRAY(K:K1-1)
               ENDIF
               IF (OUTUNIT.EQ.100) WRITE(OUTUNIT,*)'FILENAME=',FILENAME 
               DO L=K1,100
                   IF (CH_ARRAY(L:L).NE.' ') GO TO 100
               ENDDO
 100           DO M=L,100
                   IF (CH_ARRAY(M:M).EQ.' ') GO TO 110
               ENDDO
      !... extract unit number
 110           UNIT=CH_ARRAY(L+3:M-1)
               LENGTH=M-1-L-3+1
               IF (OUTUNIT.EQ.100) WRITE(OUTUNIT,*) 'UNIT=',UNIT

      !... convert unit from string to integer
               CALL CHAR_TO_INTEGER(UNIT,LENGTH,UNITNO)        
      !... open file with assigned unit number. Note unit 2 and 99
      !... is assigned to binary files
               IF (UNITNO.EQ.2) THEN
                      OPEN(UNITNO,FILE=FILENAME,FORM='UNFORMATTED')
               ELSE 
                   IF (UNITNO.EQ.99) THEN
                       OPEN(UNITNO,FILE=FILENAME,STATUS='OLD',
     >                               FORM='UNFORMATTED',ERR=300)
                       go to 50
 300                   continue 
                   ELSE
                       OPEN(UNITNO,FILE=FILENAME)
                   ENDIF
               ENDIF
       
               
         ENDIF   !end of if with checking "assign"
                     
      ENDIF      !end of if with checking "$"
     
      GOTO 50
 60   RETURN
      END


C:::::::::::::::::::::CHAR_TO_INTEGER::::::::::::::::::::::::::::::::::::
C.... This routine is used to convert string to integer.
C.... It is called in GET_INOUT_FILES().


      SUBROUTINE CHAR_TO_INTEGER(INCHAR, !... input string
     >                           LENGTH, !... length of INCHAR
     >                           OUTINT) !... integer out

      CHARACTER*10 INCHAR    ! input string
      INTEGER      OUTINT    ! converted integer
      INTEGER      DIGIT     ! each digit in string
      CHARACTER*10 NEWCHAR   ! temp variable
      M=LENGTH
      OUTINT=0
      NEWCHAR=INCHAR
      !... get each digit and convert to integer
      DO I=1,LENGTH
        DIGIT=ICHAR(NEWCHAR)-48
        M=M-1        
        OUTINT=OUTINT+DIGIT*(10**M)        
        NEWCHAR=INCHAR(I+1:LENGTH)
      ENDDO
      
      RETURN
      END

C:::::::::::::::::::::GET_CONTR_PARAMS::::::::::::::::::::::::::::::::::::
C.... This routine is used to extract control parameter values from
C.... the control file (HEUVAC.bat) and write them into files
C.... instead of input from keyboard. 
C.... Use &=START=& and &=END=& to identify the range of data.
C.... Use ! to identify the data in a line
C.... This routine is called in OPEN_FILE() routine.

      SUBROUTINE GET_CONTR_PARAMS(INUNIT,OUTUNIT)
      INTEGER INUNIT                 ! Input unit 
      INTEGER OUTUNIT                ! Output unit 
         
      CHARACTER*200 LINE             ! Line buffer
      
      !... read each line from input file until find out flag
  50      READ(INUNIT,'(A200)',END=100) LINE
      DO I=1,200
        IF(INDEX(LINE,'&=START=&').NE.0) GO TO 60 
      ENDDO
      GO TO 50
      !... parse the line and pick out the data
  60  READ(INUNIT,'(A200)',END=100) LINE
      
      !... if meet end flag,return
      IF(INDEX(LINE,'&=END=&').NE.0) GO TO 100
      !... use "!" as a flag to identify data
      I1=INDEX(LINE,'!')
      !... output data into output file
      IF(I1.NE.0) THEN
         WRITE(OUTUNIT,*) LINE(1:I1-1)
      ENDIF    
      GO TO 60  
  100 RETURN 
      END                                                                        
                 
